/*
 * 360.cpp
 *
 *  Created on: Feb 25, 2014
 *      Author: killerg
 */




#include "../bill_deps.h"
#include "platform_deps.h"

#include <list>
#include <map>
#include <string>

static const int app_id = 201306956;
static const char* app_key = "fee302ddf2f1bf1be029e0581771c4b1";
static const char* app_secret = "08aef7138646b3846536755d14665b58";

static void send_result(
        http_server::p_request req,
        const char* msg) {
	if (strcmp("ok", msg)) {
        auto uri = evhttp_request_get_evhttp_uri(req->req);
        auto query = evhttp_uri_get_query(uri);
        char buff[100];
        time_t now = time(NULL);
        strftime(buff,100,"%Y%m%d_%H%M%S",localtime(&now));
    	UTIL_LOG(E, "%s EPI_360 msg:%s, query:%s", buff, msg, query);
	}
    req->send_reply(200, msg);
}

bool init_360() {
    g_https->handle("/bill/360", [](http_server::p_request req){
    	if (g_svrmgr->db_set >= 100) {
    		send_result(req, "retry later");
        	return;
    	}

    	stringstream ss;
    	for (auto &itr : req->query) {
    		if (itr.first == "sign_return") {
    			continue;
    		} else if (itr.first == "sign") {
    			continue;
    		} else {
    			ss << itr.second << "#";
    		}
    	}
    	ss << app_secret;

        string a_gateway_flag = req->query["gateway_flag"];
        if (a_gateway_flag.empty() || a_gateway_flag == "" || a_gateway_flag != "success") {
            send_result(req, "ok");
            return;
        }


#define CHECK_ARG(name)	\
    	string a_##name = req->query[#name];	\
    	if (a_##name.empty() || a_##name == "") {	\
    		send_result(req, "need "#name);	\
    		return;	\
    	}

    	CHECK_ARG(app_key);
    	CHECK_ARG(product_id);
    	CHECK_ARG(amount);
    	CHECK_ARG(app_uid);
    	CHECK_ARG(order_id);
    	CHECK_ARG(sign_type);
    	CHECK_ARG(sign_return);
    	CHECK_ARG(sign);
    	CHECK_ARG(app_ext1);
    	CHECK_ARG(user_id);

    	if (Md5(ss.str().c_str()) != a_sign) {
    		send_result(req, "sign err");
    		return;
    	}

    	if (a_app_key != app_key) {
    		send_result(req, "app_key err");
    		return;
    	}

    	if (a_sign_type != "md5") {
    		send_result(req, "sign_type err");
    		return;
    	}

    	string account = fmt_cstr("360-%s", a_user_id.c_str());
        string orderId = fmt_cstr("360-%s", a_order_id.c_str());
        double payFee = atoi(a_amount.c_str()) * g_cfg->Rate / 100.0f;
        int serverid = -1;
        string machinecode;

        std::vector<std::string> vNote;
        split(a_app_ext1, ",", vNote);
		if (vNote.size() != 2) {
    		send_result(req, "app_ext1 err");
            return;
		}
		serverid = atoi(vNote[0].c_str());
		machinecode = vNote[1];

        if (payFee <= 0) {
            send_result(req, "amount err");
            return;
        }

        if (serverid <= 0) {
            send_result(req, "app_ext1 err");
            return;
        }

        auto playerinfo = g_svrmgr->get_player_info(serverid, account);
        if (NULL == playerinfo) {
        	send_result(req, "no such player");
            return;
        }

        g_svrmgr->post_order(serverid, orderId, playerinfo->playerid, payFee, machinecode, [req](bool ok){
            if (ok)
            	send_result(req, "ok");
            else
            	send_result(req, "error");
        });

    });

    return true;
}
